<?php
class item_passavatar extends template_item
{
	var $default_name = "Pass an Avatar";
	var $default_description = "This item will allow you to give another person an avatar. {status}";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['unused'] = "No Avatar Stored, use item once to store an avatar.";
			$this->item_config['used'] = "An Avatar Stored, use item to make the avatar yours.";
			$this->item_config['upload'] = 1;
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{status} = Status to state if a message is stored or not<br />Status Message is settable below.",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Unused status message</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][unused]", $this->item_config['unused']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Used status message</b>",
															$this->ipsclass->adskin->form_input("item[".$this->item['item_id']."][used]", $this->item_config['used']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Allow uploading the avatar?</b>",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][upload]", $this->item_config['upload']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['unused'] = $input['unused'];
		$save['used'] = $input['used'];
		$save['upload'] = intval($input['upload']) & 1;
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_ucp');
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
 		list( $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height']) = explode ("x", $this->ipsclass->vars['avatar_dims']);
 		$this->ipsclass->lang['av_allowed_files'] = sprintf($this->ipsclass->lang['av_allowed_files'], implode (' .', explode( "|", $this->ipsclass->vars['avatar_ext'] ) ) );
		$this->ipsclass->lang['av_text_url'] = sprintf( $this->ipsclass->lang['av_text_url'], $this->ipsclass->vars['av_width'], $this->ipsclass->vars['av_height'] );
		$this->ipsclass->lang['av_text_upload'] = sprintf( $this->ipsclass->lang['av_text_upload'], $this->ipsclass->vars['avup_size_max'] );
 		
 		if ( $this->ipsclass->vars['allow_flash'] != 1 )
		{
			$this->ipsclass->lang['av_allowed_files'] = str_replace( ".swf", "", $this->ipsclass->lang['av_allowed_files'] );
		}
		
		$upload = "";
		if($this->item_config['upload'])
		{
			$upload = <<<EOF
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['select_an_image_file']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='file' size='30' name='avatar_upload' />
			</td>
		</tr>
		<tr>
			<td class='darkrow3' align='center' colspan='2'>
				{$this->ipsclass->lang['or']}
			</td>
		</tr>
EOF;
		}
		else
		{
			$this->ipsclass->lang['av_text_upload'] = "";
			$this->ipsclass->lang['av_allowed_files'] = "";
		}
		
		$size_html = "";
		if($this->ipsclass->vars['disable_ipbsize'])
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='right'>
			<b>{$this->ipsclass->lang['width']}</b>
			&nbsp;
			<input type='text' size='3" name='man_width' />
			&nbsp;&nbsp;
			<b>{$this->ipsclass->lang['height']}</b>
			&nbsp;
			<input type='text' size='3' name='man_height' /></td>
	</tr>
EOF;
		}
		else
		{
			$size_html = <<<EOF
	<tr>
		<td class='row1' align='left'>
			&nbsp;
		</td>
		<td class='row2' align='left'>
			<b>{$this->ipsclass->lang['av_autosize']}</b>
			<br />
			({$this->ipsclass->lang['av_as_desc']})
		</td>
	</tr>
EOF;
		}
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['upload_avatar']}</div>
<form action='{$this->base_url}' method='post' enctype='multipart/form-data'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left' colspan='2'>
				{$this->ipsclass->lang['av_text_url']} {$this->ipsclass->lang['av_text_upload']}
				<br />
				{$this->ipsclass->lang['av_allowed_files']}
			</td>
		</tr>
{$upload}
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['enter_an_image_url']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='text' size='40' name='avatar_url' />
			</td>
		</tr>				
{$size_html}
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['store_avavtar']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done($inventory_ids = array())
	{
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		if(is_array($setting) && $setting['avatar'])
		{
			return true;
		}
		return isset($this->ipsclass->input['avatar_url']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$inv_id = reset($inventory_ids);
		$setting = unserialize($this->ipsclass->market_class->inventory_id[$inv_id]['settings']);
		
		require ROOT_PATH."sources/lib/func_usercp.php";
		$ucp = new func_usercp();
		$ucp->ipsclass = &$this->ipsclass;
		
		require_once(KERNEL_PATH."class_upload.php");
		$upload = new class_upload();
		$upload->ipsclass = &$this->ipsclass;
		
		require_once( KERNEL_PATH.'class_image.php' );
		$image = new class_image();
		$image->ipsclass = &$this->ipsclass;
		
		if(is_array($setting) && $setting['avatar'])
		{
			$ucp->bash_uploaded_avatars($this->ipsclass->member['id']);
			
			$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => $setting['avatar'], 'avatar_size' => $setting['dimensions'], 'avatar_type' => 'url' ), 'id='.$this->ipsclass->member['id'] );
		
			$this->delete_items($inventory_ids);
			
			$this->_write_log($inv_id, "Changed avatar");
			
			$this->ipsclass->market_class->redirect('avatar_updated',$this->ipsclass->base_url.'act=UserCP&CODE=24',array('url'=>1));
		}
		else
		{
			if ( preg_match( "/^http:\/\/$/i", $this->ipsclass->input['avatar_url'] ) )
			{
				$this->ipsclass->input['avatar_url'] = "";
			}
			
			if ( preg_match( "#javascript:#is", $this->ipsclass->input['avatar_url'] ) )
			{
				$this->ipsclass->input['avatar_url'] = "";
			}
			
			if ( $this->ipsclass->xss_check_url( $this->ipsclass->input['avatar_url'] ) !== TRUE )
			{
				$this->ipsclass->input['avatar_url'] = '';
			}
			
			if ( (empty($this->ipsclass->input['avatar_url']) or $this->ipsclass->input['avatar_url'] == "") && $this->item_config['upload'] )
			{
				if ( preg_match( "#javascript:#is", $_FILES['upload_avatar']['name'] ) )
				{
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_upload' ) );
				}
				
				if ($_FILES['avatar_upload']['name'] == "" or $_FILES['avatar_upload']['name'] == "none" )
				{
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_upload' ) );
				}
				
				$exts = array();
				foreach(explode( ",", $this->ipsclass->vars['avatar_ext'] ) as $ext)
				{
					$exts[] = trim($ext);
				}
				
				require_once(KERNEL_PATH."class_upload.php");
				$upload = new class_upload();
				$upload->ipsclass = &$this->ipsclass;
				
				require_once( KERNEL_PATH.'class_image.php' );
				$image = new class_image();
				$image->ipsclass = &$this->ipsclass;
				
				$upload->out_file_name = 'av-item-'.$inv_id;
				$upload->out_file_dir = $this->ipsclass->vars['upload_dir'];
				$upload->max_file_size = ($this->ipsclass->vars['avup_size_max'] * 1024) * 8;
				$upload->allowed_file_ext = $exts;
				$upload->upload_form_field = 'avatar_upload';
				
				$ucp->bash_uploaded_avatars($this->ipsclass->member['id']);
				
				$real_name = 'av-'.$this->ipsclass->member['id'];
				$real_type = 'upload';
				
				$upload->upload_process();
					
				if($upload->error_no)
				{
					$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => '', 'avatar_size' => '', 'avatar_type' => '' ), 'id='.$this->ipsclass->member['id'] );
					
					switch( $upload->error_no )
					{
						case 1:
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
						case 2:
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_av_type' ) );
						case 3:
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_to_big') );
						case 4:
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
						case 5:
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_failed' ) );
					}
				}
				
				list($p_width, $p_height) = explode( "x", strtolower($this->ipsclass->vars['avatar_dims']) );
				
				$real_name = $upload->parsed_file_name;
				
				if ( ! $this->ipsclass->vars['disable_ipbsize'] and $upload->file_extension != '.swf' )
				{
					$image->in_type        = 'file';
					$image->out_type       = 'file';
					$image->in_file_dir    = $this->ipsclass->vars['upload_dir'];
					$image->in_file_name   = $real_name;
					$image->out_file_name  = 'av-item-'.$inv_id;
					$image->desired_width  = $p_width;
					$image->desired_height = $p_height;
					
					$return = $image->generate_thumbnail();
		
					$im['img_width']  = $return['thumb_width'];
					$im['img_height'] = $return['thumb_height'];
					
					if ( isset($return['thumb_location']) AND strstr( $return['thumb_location'], 'avs-' ) )
					{
						@unlink( $this->ipsclass->vars['upload_dir']."/".$real_name );
						
						$real_name = 'av-item-'.$inv_id.'.'.$image->file_extension;
						
						@rename( $this->ipsclass->vars['upload_dir']."/".$return['thumb_location'], $this->ipsclass->vars['upload_dir']."/".$real_name );
						@chmod(  $this->ipsclass->vars['upload_dir']."/".$real_name, 0777 );
					}
				}
				else
				{	
					$w = intval($this->ipsclass->input['man_width'])  ? intval($this->ipsclass->input['man_width'])  : $p_width;
					$h = intval($this->ipsclass->input['man_height']) ? intval($this->ipsclass->input['man_height']) : $p_height;
					$im['img_width']  = $w > $p_width  ? $p_width  : $w;
					$im['img_height'] = $h > $p_height ? $p_height : $h;
				}
				
				if ( filesize( $this->ipsclass->vars['upload_dir']."/".$real_name ) > ($this->ipsclass->vars['avup_size_max']*1024))
				{
					@unlink( $this->ipsclass->vars['upload_dir']."/".$real_name );
					
					$this->ipsclass->DB->do_update( 'member_extra', array( 'avatar_location' => '', 'avatar_size' => '', 'avatar_type' => '' ), 'id='.$this->ipsclass->member['id'] );
					
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'upload_to_big' ) );
				}
				
				$real_choice = $this->ipsclass->vars['upload_url']."/".$real_name;
				$real_dims   = $im['img_width'].'x'.$im['img_height'];
			}
			else
			{
				$this->ipsclass->input['avatar_url'] = trim($this->ipsclass->input['avatar_url']);
				
				if ( empty($this->ipsclass->vars['allow_dynamic_img']) )
				{
					if ( preg_match( "/[?&;]/", $this->ipsclass->input['avatar_url'] ) )
					{
						$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'avatar_invalid_url' ) );
					}
				}
				
				//-----------------------------------------
				// Check extension
				//-----------------------------------------
				
				$ext = explode ( ",", $this->ipsclass->vars['avatar_ext'] );
				$checked = 0;
				$av_ext = preg_replace( "/^.*\.(\S+)$/", "\\1", $this->ipsclass->input['avatar_url'] );
				
				foreach ($ext as $v )
				{
					if (strtolower($v) == strtolower($av_ext))
					{
						if ( ( $v == 'swf' ) AND ($this->ipsclass->vars['allow_flash'] != 1) )
						{
							$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'no_flash_av' ) );
						}
						
						$checked = 1;
					}
				}
				
				if ($checked != 1)
				{
					$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'avatar_invalid_ext' ) );
				}
				
				//-----------------------------------------
				// Check image size...
				//-----------------------------------------
				
				$im = array();
				
				if ( ! $this->ipsclass->vars['disable_ipbsize'] )
				{
					if ( ! $img_size = @GetImageSize( $this->ipsclass->input['avatar_url'] ) )
					{
						$img_size[0] = $p_width;
						$img_size[1] = $p_height;
					}
					
					$im = $this->ipsclass->scale_image( array(
													'max_width'  => $p_width,
													'max_height' => $p_height,
													'cur_width'  => $img_size[0],
													'cur_height' => $img_size[1]
										   )      );
				}
				else
				{	
					$w = intval($this->ipsclass->input['man_width'])  ? intval($this->ipsclass->input['man_width'])  : $p_width;
					$h = intval($this->ipsclass->input['man_height']) ? intval($this->ipsclass->input['man_height']) : $p_height;
					$im['img_width']  = $w > $p_width  ? $p_width  : $w;
					$im['img_height'] = $h > $p_height ? $p_height : $h;
				}
				
				//-----------------------------------------
				// Remove any uploaded images..
				//-----------------------------------------
				
				$real_choice = $this->ipsclass->input['avatar_url'];
				$real_dims   = $im['img_width'].'x'.$im['img_height'];
			}
			$settings = array('avatar' => $real_choice, 'dimensions' => $real_dims);
			$this->ipsclass->DB->do_update('market_inventory',array(
																		'settings' => serialize($settings),
											),'i_id='.$inv_id		);
			
			$this->ipsclass->market_class->redirect('avatar_stored',"");
		}
	}
	
	function parse_store_description()
	{
		return str_replace('{status}','',$this->item['item_description']);
	}
	
	function parse_group_description()
	{
		return str_replace('{status}','',$this->item['item_description']);
	}
	
	function parse_single_description($inv_item=array())
	{
		$setting = unserialize($inv_item['settings']);
		return str_replace('{status}',$setting['avatar'] ? $this->item_config['used'] : $this->item_config['unused'],$this->item['item_description']);
	}
}
?>